var timer;

function setTime(obj){
    var h = 0,
        m = 0,
        s = 0;
    timer = setInterval(function (){
        if(s < 10){
            //如果秒数少于10在前面加上0
            $('#time').html('0' + h + ':0' + m + ':0' + s);
        } else if(m < 10){
            $('#time').html('0' + h + ':0' + m + ':' + s);
        } else if(h < 10){
            $('#time').html('0' + h + ':' + m + ':' + s);
        } else {
            $('#time').html('0' + h + ':' + m + ':' + s);
        }
        s++;
        if(s > 59){
            m++;
            s = 0;
        }
        ;
        if(m > 59){
            h++;
            m = 0;
        }
    }, 1000)
    $(obj).hide();
    $(".hangup").show();
};

function setOutTime(obj){
    $(obj).hide();
    $(".dialing-btn").show();
    clearInterval(timer)
    $('#time').html('00:00:00');
};

(function (D, W){
    D._mix(D, {
        /**
         * 获取当前选中的客户电话号码，然后调用打电话接口拨打电话
         * customerIds 客户ID集合
         * type 类型 0 订单，1 商机，2，客户 3 线索 4 白名单 5 随意拨打 6 事业部库 7 生产 9退款
         * ids 标识
         * customerIds 客户ID （0，1，2，4，6，7 必传）
         * customerNames 客户名称 （0，1，2，4，6，7 必传）
         * widelyMonth （线索月份）
         * phone 电话号码
         * */
        //sysInfo.basePath+"/call/call_index.shtml
        dialing: function (type, ids, phone, customerIds, customerNames, widelyMonth){
            if(undefined === widelyMonth){
                widelyMonth = "";
            }
            var parentId = window.parent.document.getElementById("call");
            $.ajax({
                type: "post",
                data: {
                    ids: ids,
                    type: type,
                    phone: phone,
                    widelyMonth: widelyMonth,
                    customerIds: customerIds,
                    customerNames: customerNames
                },
                url: sysInfo.basePath + "/zhihu_call/call_index.shtml",
                success: function (data){
                    $(parentId).show();
                    $(parentId).empty().append(data);
                }
            });
        },
        closeDialing: function (){
            $('.dialing').remove();
            $('#call').hide();
        },
        /**
         * 选择短信模板
         */
        runSmsTemplate: (function (){
            $(document).on("click", ".template-body a", function (){
                var template = $(this).text();
                $(".template-textarea").val(template);
            })
        })(),
        /**
         * 解决layer弹窗回车问题
         */
        runWindowReturn: (function (){
            $(document).keydown(function (event){
                var isBlur = $(event.target).hasClass('edit-msg')
                var ev = document.all ? window.event : event;
                if(ev.keyCode == 13 && !isBlur){
                    return false
                }
            });
        })()
    });
    D.attrJudge('layer', function (){
        /**
         * 对话框扩展
         * @options  {icon:{-1:'默认没有图标',0:'感叹号',1：'勾正确',2:'叉错误',3:'问号'，4：'锁符号',5：'红哭脸',6：'绿笑脸',N>6:'感叹号'}}
         */
        D._mix(D, {
            layer: layer,
            Layer: {
                message_lod: function (msg, icon, endFunction){
                    /**
                     * msg:提示消息内容
                     * icon:提示图标; 0 警告提示图标; 1 成功提示图标; 2 错误提示图标; 5 失败提示图标;
                     * */
                    layer.msg(msg, {
                        icon: icon,
                        skin: 'layer-message',
                        end: function (index, val){
                            if(endFunction != undefined){
                                endFunction(index, val);
                            }
                        }
                    });
                },
                confirm_old: function (tit, content, size, yesFunction, cancelFunction){
                    /**操作提示层
                     *tit:弹窗标题
                     * content：弹窗提示内容,可以为html字符串
                     * yesFunction:确定按钮回调函数,必填但可以为空
                     * */
                    var htm = '<div class="layer-confirm">' +
                        '<p>' + content + '</p>' +
                        '</div>';
                    var index = layer.confirm(htm, {
                        title: tit,
                        skin: 'define-layer',
                        area: size,
                        resize: false,
                        btn: ['确定', '取消'],
                        yes: function (index, val){
                            Layer.loading();
                            if(yesFunction != undefined){
                                yesFunction(index, val);
                            } else {
                                layer.close(index);
                            }
                        },
                        btn2: function (index, val){
                            if(typeof cancelFunction == 'function'){
                                cancelFunction(index, val);
                            } else {
                                layer.close(index);
                            }
                        }
                    });
                    return index
                },
                open_old: function (tit, url, size){
                    /* *iframe:弹层,
                     * tit:弹层标题,
                     * url:iframe的url,
                     * wi,hi:弹窗的宽度与高度
                     * callback:确定按钮回调函数,必填但可以为空
                     * */
                    var index = layer.open({
                        type: 2,
                        title: tit,
                        skin: 'define-layer',
                        shade: 0.8,
                        area: size,
                        resize: false,
                        btn: ['保存', '取消'],
                        content: url,
                    });
                    return index
                },
                openPage_old: function (title, url, area, onEnd){
                    var index = layer.open({
                        type: 2,
                        shade: 0.5,
                        anim: 1,
                        skin: 'define-normal-layer',
                        title: title,
                        area: area,
                        content: url
                        , end: function (index, layero){
                            if(typeof onEnd == 'function'){
                                onEnd(index, layero);
                            }
                        }
                    });
                    return index
                },
                capture_old: function (tit, con, size, btn1, btn2, yesFunction, endFunction, val, index){
                    var index = layer.open({
                        type: 1,
                        title: tit,
                        skin: 'define-layer',
                        area: size,
                        resize: false,
                        btn: [btn1, btn2],
                        yes: function (index, val){
                            if(yesFunction != undefined){
                                yesFunction(index, val);
                            } else {
                                layer.close(index);
                            }
                        },
                        end: function (){
                            if(endFunction != undefined){
                                endFunction(index, val);
                            } else {
                                $("#" + con).hide()
                            }
                            /*$("#" + con).hide();*/

                        },
                        content: $("#" + con)
                    });
                    return index
                },
                captureHtml_old: function (tit, con, size, btn1, btn2, yesFunction, index, noFunction){
                    /*var isId=con.indexOf('#');
                    if(isId==0) {
                        con=$("#" + con);
                    }*/
                    var index = layer.open({
                        type: 1,
                        title: tit,
                        skin: 'define-layer',
                        area: size,
                        resize: false,
                        btn: [btn1, btn2],
                        yes: function (index){
                            if(yesFunction != undefined){
                                yesFunction(index);
                            } else {
                                layer.close(index);
                            }
                        }, end: function (index){
                            if(noFunction != undefined){
                                noFunction(index);
                            } else {
                                layer.close(index);
                            }
                        },
                        content: con
                    });
                    return index
                },
                showLayer_old: function (tit, id, size){
                    var index = layer.open({
                        type: 1,
                        title: tit,
                        skin: 'define-layer',
                        area: size,
                        resize: false,
                        content: $("#" + id),
                        end: function (){
                            $("#" + id).hide();
                        }
                    })
                    return index
                },
                openHtml_old: function (title, htm, area, onEnd){
                    var maxHei = $(window).height() - 180;
                    var index = layer.open({
                        type: 1,
                        shade: 0.8,
                        anim: 1,
                        skin: 'define-normal-layer',
                        title: title,
                        area: area,
                        content: htm
                        , end: function (index, layero){
                            if(typeof onEnd == 'function'){
                                onEnd(index, layero);
                            }
                        },
                        success: function (layero, index){
                            index = index;
                        }
                    });
                    $('.layui-layer-content').css('maxHeight', maxHei);
                    return index
                },
                Msg_old: {
                    succeed: function (msg, call, time){
                        layer.msg(msg, {
                            icon: 1
                        });
                        if(typeof call == 'function'){
                            time = time || msg.length / 50;
                            time = time < 1 ? 1 : time;
                            setTimeout(function (){
                                call();
                            }, time * 1000);
                        }
                    },
                    errors: function (msg){
                        layer.msg(msg, {
                            icon: 2
                        });
                    },
                    warning: function (msg){
                        layer.msg(msg, {
                            icon: 0
                        });
                    }
                },


                alert: function (a, b, c){
                    var d = $.isFunction(b);
                    return d && (c = b), D.layer.open($.extend({
                        content: a,
                        title: !1,
                        fixed: false,
                        move: false,
                        shadeClose: !0,
                        skin:'layer-alerts',
                        closeBtn:2,
                        yes: function (a, v){
                            if(c && $.isFunction(c)){
                                var f = c.call(this, a, v);
                                (f || "undefined" == typeof f) && D.layer.close(a);
                                $(v).find(".layui-form").length > 0 && layui.use(["form"], function (){
                                    layui.form().render()
                                });
                            } else D.layer.close(a);
                        }
                    }, d ? {} : b))
                },
                /**
                 * @param msg
                 * @param options
                 * @param yes
                 * @returns {*}
                 */
                message: function (a, b, c){
                    var e, d = $.isFunction(b);
                    d && (c = b), e = $.extend({}, {
                        time: 2e3,
                        fixed: false,
                        move: false,
                        skin: 'layer-warning',
                        closeBtn:1,
                    }, b || {}),
                        D.layer.msg(a||'操作失败，请联系管理员！', e, function (){
                            c && $.isFunction(c) && c.call(this)
                        })

                },
                /**
                 *
                 * @param msg
                 * @param options
                 * @param ok
                 * @param cancel
                 * @returns {*}
                 */
                confirm: function (a, b, c, d){
                    var e = $.isFunction(b);
                    return e && (d = c, c = b), D.layer.open($.extend({
                        type: 1,
                        closeBtn: 2,
                        content: a,
                        fixed: false,
                        move: false,
                        title: !1,
                        btn: ["&#x786E;&#x5B9A;", "&#x53D6;&#x6D88;"],
                        btn2: d,
                        skin: 'layui-layer-dialog layer-confire',
//                        skin: 'layer-confire',
                        yes: function (a, v){
                            if(c && $.isFunction(c)){
                                var f = c.call(this, a, v);
                                (f || "undefined" == typeof f) && D.layer.close(a);
                                $(v).find(".layui-form").length > 0 && layui.use(["form"], function (){
                                    layui.form().render()
                                });
                            } else D.layer.close(a);
                        }
                    }, e ? {} : b))
                },
                /**
                 *
                 * @param a
                 * @param b
                 */
                open: function (a, b, c, d){
                    var e = $.isFunction(b);
                    e && (d = c, c = b);
                    var o = $.extend({
                        type: 2,
                        closeBtn: 2,
                        content: a,
                        fixed: false,
                        move: false,
                        title: !1,
                        btn: ["&#x786E;&#x5B9A;", "&#x53D6;&#x6D88;"],
                        btn2: d,
                        skin: 'define-layer,layui-layer-dialog layer-confire',
                        yes: function (a, v){
                            if(c && $.isFunction(c)){
                                var $iframe = $("#layui-layer-iframe" + a).contents(),
                                    f = c.call(this, a, $iframe, v);
                                (f || "undefined" == typeof f) && D.layer.close(a);
                                $(v).find(".layui-form").length > 0 && layui.use(["form"], function (){
                                    layui.form().render()
                                });
                            } else D.layer.close(a);
                        }
                    }, e ? {} : b);
                    if(o.type == 6){
                        b.type == 1;
                        o = $.extend(o, {
                            type: 1,
                            closeBtn: 2,
                            move: false,
                            fixed: false,
                            mode: b.mode ? b.mode || 'get' : "post",
                            data: b.data || {},
                            skin: 'layui-layer-dialog layer-confire',
                            yes: function (a, v){
                                if(c && $.isFunction(c)){
                                    var f = c.call(this, a, v);
                                    (f || "undefined" == typeof f) && D.layer.close(a);
                                    $(v).find(".layui-form").length > 0 && layui.use(["form"], function (){
                                        layui.form().render()
                                    });
                                } else D.layer.close(a);
                            }
                        }, e ? {} : b);
                        $.ajax({
                            url: a,
                            type: o.mode,
                            dataType: 'html',
                            data: o.data,
                        })
                            .done(function (a){
                                o.content = a;
                                D.layer.open(o);
                            })
                            .fail(function (){
                                /*console.log("error");*/
                            })
                            .always(function (){
                                /*console.log("complete");*/
                            });
                        return;
                    }
                    return D.layer.open(o);
                },
                loading: {
                    start: function (){
                        var e = $.extend({
                            shade: 0.1
                        }, {});

                        D.Layer.loading.index = layer.load(3, e);
                    },
                    done: function (){
                        D.layer.close(D.Layer.loading.index);
                    }
                },
                showImage: function (a, b){
                    $(document).on('click', a, function (){
                        // 设置data-blank="true" 新开窗口
                        var blank=$(this).attr('data-blank');
                        if(blank)return window.open(this.src),!1;
                        // 普通弹框
                        var c = $(this).attr('src');
                        if ( b != undefined && b.parent) return window.parent.DGG.layer.open($.extend({
                            content:'<img src="' + c + '" alt="">',
                            shadeClose:!0,
                            closeBtn:0,
                            title:!1,
//                            area:[ "60%" ],
                            type:1,
                            skin:"dgg-show-image"
                        }, b)), !1;

                        D.layer.open($.extend({
                            content: '<img src="' + c + '" alt="">',
                            shadeClose: !0,
                            closeBtn: 0,
                            title: false,
//                            area: ["80%","80%"],

                            type: 1,
                            skin: "dgg-show-image "
                        }, b))
                    });
                    $(document).on('click', '.dgg-show-image ', function() {
                        layer.closeAll();
                    });


                },
                showImages: function (a, b){

                    return typeof a == "string" ? ($("body").on("click", a + " " + "img", function (){
                        var c = $(this),
                            d = {title: "",
                                id: 123,
                                start: $(a + ' ' + "img").index(c) || 0, data: []};
                        $.each(c.parents(a).find("img"), function (a, b){
                            d.data.push({
                                alt: "",
                                pid: 666,
                                src: $(b).attr("src"),
                                thumb: $(b).attr("src")
                            })
                        }), D.layer.photos($.extend({
                            photos: d, closeBtn: 0, shade: .5, shift: 5, skin: "dgg-show-photos"
                        }, b))
                    }), void 0) : (D.layer.photos({
                        photos: a, closeBtn: 0, shade: .5, shift: 5, skin: "dgg-show-photos"
                    }), void 0);
                }
            },
        });
    });
})(DGG, window);